package com.wzq.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.dto.SpaceDto;
import com.wzq.dao.pojo.*;
import com.wzq.service.ISpaceService;
import com.wzq.service.IWechat_userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
    @RequestMapping("/PC/space")
public class PCSpaceController extends GetSome {

    @Autowired
    private ISpaceService iSpaceService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Space space) {
        LambdaQueryWrapper<Space> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Space::getW_id,space.getW_id());
        lambdaQueryWrapper.eq(Space::getSpace_name,space.getSpace_name());
        lambdaQueryWrapper.eq(Space::getP_id,space.getP_id());
        Space SelectSpaceName = iSpaceService.getOne(lambdaQueryWrapper);
        if (SelectSpaceName != null){
            iSpaceService.removeById(SelectSpaceName.getId());
            return new Result(Code.DELETE_OK,null,"取消收藏！");
        }else{
            iSpaceService.save(space);
            return new Result(Code.SAVE_OK,null,"收藏成功！");
        }
    }
//    是否收藏
    @PostMapping("/{w_id}/getfavorites/{p_id}")
    public Result getfavorites(@PathVariable Integer w_id, @PathVariable Integer p_id) {
        List<Space> spaces =new ArrayList<>();
        //查询该用户的所有实验室
        List<Space> list = iSpaceService.getSpaceByUserIdNoPid(w_id);
        //查询实验室中是否存在该项目
        for ( Space s : list ) {
            s.setP_id(p_id);
            LambdaQueryWrapper<Space> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Space::getW_id,w_id).eq(Space::getSpace_name,s.getSpace_name());
            lqw.eq(Space::getP_id,p_id);        //.eq(Space::getIsfavorites,1)
            // 存在赋值1，不存在赋值0
            if ( iSpaceService.count(lqw)>0)   s.setIsfavorites(1);
            else        s.setIsfavorites(0);
            // 添加到集合中
            spaces.add(s);
        }
        return new Result(Code.GET_OK,spaces);
    }

    //    id删除
    @GetMapping("/{w_id}/delete/{p_id}")
    public Result deleteById(@PathVariable Integer w_id,@PathVariable Integer p_id,@RequestParam String spacename) {
        LambdaQueryWrapper<Space> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Space::getW_id,w_id).eq(Space::getP_id,p_id).eq(Space::getSpace_name,spacename);
        boolean flag = iSpaceService.remove(lqw);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }


//    //是否收藏
//    @PostMapping("isfavorites")
//    public Result IsFavorites(@RequestBody Space space){
//        List<Space> list =  iSpaceService.isfavorites(space.getW_id(), space.getP_id());
//        if (list==null){
//            return new Result(Code.SAVE_ERR,"查询收藏失败");
//        }
//        return new Result(Code.GET_ERR,list,"查询收藏成功");
//    }

    //添加空间内的项目
    @PostMapping("/spaceProject")
    public Result spaceProject(@RequestBody Space space){
       Boolean flag = iSpaceService.save(space);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Space space) {
        boolean flag = iSpaceService.updateById(space);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    //   重命名
    @PutMapping("/rename")
    public Result reName(@RequestBody SpaceDto spaceDto) {
        LambdaUpdateWrapper<Space> luw = new LambdaUpdateWrapper<>();
        luw.set(Space::getSpace_name,spaceDto.getSpace_name());
        luw.eq(Space::getW_id,spaceDto.getW_id()).eq(Space::getSpace_name,spaceDto.getOldname());
        boolean flag = iSpaceService.update(luw);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }


    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iSpaceService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //   按用户id 空间名字 删除空间
    @GetMapping("/deleteSpaceByIdAndSpaceName")
    public Result deleteById(@RequestParam("w_id") String w_id,@RequestParam("spacename") String spacename) {
        int i =  iSpaceService.deleteSpaceByIdAndSpaceName(w_id,spacename);
        if (i!=0){
            return new Result(Code.DELETE_OK,"删除成功");
        }else{
            return new Result(Code.DELETE_ERR,"删除失败");

        }

    }

    //    根据用户ID获取该用户下的所有空间
    @PostMapping("/getSpaceByUserId")
    public Result getSpaceByUserId(@RequestBody SpaceDto spaceDto) {
        LambdaQueryWrapper<Space> lqw = new LambdaQueryWrapper();
        lqw.isNotNull(Space::getP_id);
        lqw.eq(Space::getW_id,spaceDto.getW_id()).eq(Space::getSpace_name,spaceDto.getSpace_name());
        List<Space> list = iSpaceService.list(lqw);
        Integer[] p_ids = new Integer[list.size()];
        int i=0;
        for (Space space : list) {
            p_ids[i++]=space.getP_id();
        }
        Map<String , Object> map = new HashMap<>();
        if (!(p_ids.length >0))
            return new Result(Code.GET_OK,map);
        LambdaQueryWrapper<Project> p_lqw = new LambdaQueryWrapper<>();
        p_lqw.in(Project::getId,p_ids);
        map.put("data",  iProjectService.list(p_lqw) );
        return new Result(Code.GET_OK,map);
    }

    //    根据用户ID获取该用户下的所有空间（p_id为空时 我的页面展示作用）
    @PostMapping("/getSpaceByUserIdNoPid/{w_id}")
    public Result getSpaceByUserIdNoPid(@PathVariable Integer w_id) {
        List<Space> list = iSpaceService.getSpaceByUserIdNoPid(w_id);
        Map<String , Object> map = new HashMap<>();
        map.put("data",list);
        return new Result(Code.GET_OK,map);
    }

    //    获得id对应用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Space space = iSpaceService.getById(id);
        Integer code = space != null ? Code.GET_OK : Code.GET_ERR;
        String msg = space != null ? "" : "数据查询失败，请重试！";
        return new Result(code,space,msg);
    }

    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Space> list = iSpaceService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }
    //    p_id收藏数
    @GetMapping("/favorite/{p_id}")
    public Result favoriteByPid(@PathVariable Integer p_id) {
        QueryWrapper<Space> lqw = new QueryWrapper<>();
        lqw.select("DISTINCT w_id");
        lqw.eq("p_id",p_id);
        int size = iSpaceService.count(lqw);
        return new Result(size>-1 ? Code.DELETE_OK:Code.DELETE_ERR,size);
    }
}
